package stack;

/**
 * no one is coming ,unless you change by yourself
 *
 * @author : pang-yun
 * @date : 2021/8/26
 */

//用于 数组实现 栈
public class StackSimulation {


    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(4);
        arrayStack.push(1);
        arrayStack.push(2);
        arrayStack.push(3);
        arrayStack.push(4);
        arrayStack.pop();
        arrayStack.list();
    }

    static class ArrayStack {
        private int maxSize; //最大值
        private int[] stack; // 存放数据的栈
        private int top = -1; // 栈顶  初始值为-1

        public ArrayStack(int maxSize) {
            this.maxSize = maxSize;
            stack = new int[maxSize];
        }

        //栈满
        public boolean isFull() {
            return top == maxSize - 1;
        }

        //是否为空
        public boolean isEmpty() {
            return top == -1;
        }

        //入栈
        public void push(int value) {
            if (isFull()) {
                System.out.println("已满");
                return;
            }
            top++;
            stack[top] = value;
        }

        //弹出
        public int pop() {
            if (isEmpty()) {
                System.out.println("为空！");
                throw new RuntimeException("为空！");
            }
            int i = stack[top];
            System.out.println("弹出： " + stack[top]);
            top--;
            return i;

        }

        //遍历
        public void list() {
            if (isEmpty()) {
                System.out.println("empty");
                return;
            }
            for (int i = top; i >= 0; i--) {
                System.out.println(stack[i]);
            }
        }
    }

}
